package com.tobid.adprofadapter;

import android.text.TextUtils;
import android.util.Log;

import com.windmill.sdk.WMConstants;
import com.windmill.sdk.base.WMBidUtil;
import com.windmill.sdk.custom.WMAdBaseAdapter;
import com.adprof.sdk.api.IBiddingNotify;

import java.util.HashMap;
import java.util.Map;

// https://doc.sigmob.com/tobid/21163/
// referBidInfo 字段定义说明
public class AdprofBiddingUtil {

    public static Map<String, Object> getBidingWinNoticeParam(String price, Map<String, Object> referBidInfo) {
        Map<String, Object> map = new HashMap<>();
        try {
            Log.d(AdprofInitNetwork.TAG, "win  price = " + price + "   map = " + referBidInfo);

            if (referBidInfo != null) {

                Object winnerEcpm = referBidInfo.get(WMBidUtil.WINNER_ECPM);

                if (winnerEcpm != null && Double.parseDouble(winnerEcpm.toString()) > 0) {
                    map.put(IBiddingNotify.WIN_FIRST_PRICE, String.valueOf(winnerEcpm));

                } else if (!TextUtils.isEmpty(price)) {
                    map.put(IBiddingNotify.WIN_FIRST_PRICE, price);

                } else {
                    map.putAll(referBidInfo);
                }

                Object biddingEcpm = referBidInfo.get("bidding_ecpm");

                Object waterfallEcpm = referBidInfo.get("waterfall_ecpm");

                if (waterfallEcpm != null && parse(waterfallEcpm) > 0
                        && biddingEcpm != null && parse(biddingEcpm) > 0) {

                    if (parse(waterfallEcpm) > parse(biddingEcpm)) {
                        map.put(IBiddingNotify.WIN_HIGHEST_LOSS_PRICE, waterfallEcpm);

                    } else {
                        map.put(IBiddingNotify.WIN_HIGHEST_LOSS_PRICE, biddingEcpm);
                    }

                } else if (biddingEcpm != null && parse(biddingEcpm) > 0) {
                    map.put(IBiddingNotify.WIN_HIGHEST_LOSS_PRICE, biddingEcpm);

                } else if (waterfallEcpm != null && parse(waterfallEcpm) > 0) {
                    map.put(IBiddingNotify.WIN_HIGHEST_LOSS_PRICE, waterfallEcpm);

                }
            }

        } catch (Exception e) {
            Log.d(AdprofInitNetwork.TAG, Log.getStackTraceString(e));
        }

        map.put(IBiddingNotify.MEDIATION, "tobid");

        return map;
    }

    public static Map<String, Object> getBidingLossNoticeParam(String price, Map<String, Object> referBidInfo) {
        Map<String, Object> map = new HashMap<>();

        try {

            if (referBidInfo != null) {

                Object winnerEcpm = referBidInfo.get(WMBidUtil.WINNER_ECPM);
                Object biddingEcpm = referBidInfo.get("bidding_ecpm");

                if (winnerEcpm != null && parse(winnerEcpm) > 0) {
                    map.put(IBiddingNotify.LOSS_FIRST_PRICE, winnerEcpm);

                } else if (biddingEcpm != null && parse(biddingEcpm) > 0) {
                    map.put(IBiddingNotify.LOSS_FIRST_PRICE, biddingEcpm);

                } else if (price != null && parse(price) > 0) {
                    map.put(IBiddingNotify.LOSS_FIRST_PRICE, price);

                } else {
                    map.putAll(referBidInfo);
                }

                // 13 头条  16 GDT    19 快手  21 百度  22--Gromore
                Object winnerChannel = referBidInfo.get(WMBidUtil.WINNER_CHANNEL);

                if (winnerChannel != null) {
                    map.put(IBiddingNotify.LOSS_ADN_ID, String.valueOf(winnerChannel));
                }
            }

        } catch (Exception e) {
            Log.d(AdprofInitNetwork.TAG, Log.getStackTraceString(e));
        }

        map.put(IBiddingNotify.MEDIATION, "tobid");

        return map;
    }

    private static double parse(Object price) {
        return Double.parseDouble(price.toString());
    }

    public static int getBidFloor(WMAdBaseAdapter adapter, Map<String, Object> serverExtra) {
        int floor = 0;

        try {
            return adapter.getBidFloor();

        } catch (Throwable ignore) {

            try {
                Object bidFloor = serverExtra.get(WMConstants.BID_FLOOR);
                if (bidFloor != null) {
                    floor = (Integer) bidFloor;
                }
            } catch (Throwable e) {
                Log.d(AdprofInitNetwork.TAG, Log.getStackTraceString(e));
            }
        }

        return floor;
    }
}
